//----------------------------------------------------------------------------------------
// @Project Includes
#include "includes.h"

//----------------------------------------------------------------------------------------
// @Local Macro Define in the module

//----------------------------------------------------------------------------------------
// @Local Type defines in the module

//----------------------------------------------------------------------------------------
// @Global Variables define in the module
stc_uart_t g_stcUart  = {0};

//----------------------------------------------------------------------------------------
// @Prototypes of functions in the module

//========================================================================================
/**
 * @brief  This function handles USART1 global interrupt request.
 * @param  none
 * @return none
 * @note   none
 */
void USART1_IRQHandler(void)
{
    if (USART_GetIntStatus(USART1_CH, USART_INT_RXDNE) != RESET)
    {
        /* Read one byte from the receive data register */
        g_stcUart.stcRs485.vu8rxData = USART_ReceiveData(USART1_CH);	
        g_stcUart.stcRs485.u8rxbuff[g_stcUart.stcRs485.vu32rxMQPush++] = g_stcUart.stcRs485.vu8rxData;
        if (g_stcUart.stcRs485.vu32rxMQPush >= USART_RS485_MAX_RX_SIZE)
        {
            g_stcUart.stcRs485.vu32rxMQPush = 0;
        }

//        g_stcVar.u16sleepWaitTime = g_stcParm.stcConfig.u16curTime;	
				
    }

    if (USART_GetIntStatus(USART1_CH, USART_INT_IDLEF) != RESET)
    {	
        USART_ReceiveData(USART1_CH);/*读取清标志，只做清楚标志用*/
        g_stcUart.stcRs485.brxCompleteFlg = true;
    }	
	
    if(USART_GetIntStatus(USART1_CH, USART_INT_TXDE) != RESET)
    {   
        /* Write one byte to the transmit data register */
        USART_ClrFlag( USART1_CH, USART_INT_TXDE );
		
        if (g_stcUart.stcRs485.vu32txMQPushLen > g_stcUart.stcRs485.vu32txMQPopLen)
        {
            USART_SendData(USART1_CH, g_stcUart.stcRs485.u8txbuff[g_stcUart.stcRs485.vu32txMQPopLen++]);
            if (g_stcUart.stcRs485.vu32txMQPopLen >= USART_RS485_MAX_TX_SIZE)
            {
                g_stcUart.stcRs485.vu32txMQPopLen = 0;
            }
        }
        else
        {
//						USART_ConfigInt(USART1_CH, USART_INT_TXC, ENABLE);/*最后一字节开启发送完成中断检测，用中断替代死等*/
            g_stcUart.stcRs485.vu32txMQPopLen = 0;
            g_stcUart.stcRs485.vu32txMQPushLen = 0;			
            g_stcUart.stcRs485.btxEnable = false;
            USART_ConfigInt(USART1_CH, USART_INT_TXDE, DISABLE);
//						g_stcUart.stcRs485.u8txCompleteTimeout = USART_TX_COMPLETE_MAX_TIMEOUT;/*防护专用，防止MCU异常不触发中断*/
        }
    }
//    if (USART_GetIntStatus(USART1_CH, USART_INT_TXC) != RESET)
//    {	
//        /* Write one byte to the transmit data register */
//        USART_ClrFlag( USART1_CH, USART_INT_TXC );
//				USART_ConfigInt(USART1_CH, USART_INT_TXC, DISABLE);/*关闭发送完成中断检测*/
//				g_stcUart.stcRs485.u8txCompleteTimeout = 0;/*防护专用，防止MCU异常不触发中断*/
//				RS485_RECEIVE();
//    }	
}

/**
 * @brief  This function handles USART3 global interrupt request.
 * @param  none
 * @return none
 * @note   none
 */
void USART3_IRQHandler(void)
{
    if (USART_GetIntStatus(USART3_CH, USART_INT_RXDNE) != RESET)
    {
        /* Read one byte from the receive data register */
        g_stcUart.stcSif1.vu8rxData = USART_ReceiveData(USART3_CH);	
        g_stcUart.stcSif1.u8rxbuff[g_stcUart.stcSif1.vu32rxMQPush++] = g_stcUart.stcSif1.vu8rxData;
        if (g_stcUart.stcSif1.vu32rxMQPush >= USART_SIF1_MAX_RX_SIZE)
        {
            g_stcUart.stcSif1.vu32rxMQPush = 0;
        }
    }

    if (USART_GetIntStatus(USART3_CH, USART_INT_IDLEF) != RESET)
    {	
        USART_ReceiveData(USART3_CH);/*读取清标志，只做清楚标志用*/
        g_stcUart.stcSif1.brxCompleteFlg = true;
    }	
	
    if(USART_GetIntStatus(USART3_CH, USART_INT_TXDE) != RESET)
    {   
        /* Write one byte to the transmit data register */
        USART_ClrFlag( USART3_CH, USART_INT_TXDE );
		
        if (g_stcUart.stcSif1.vu32txMQPushLen > g_stcUart.stcSif1.vu32txMQPopLen)
        {
            USART_SendData(USART3_CH, g_stcUart.stcSif1.u8txbuff[g_stcUart.stcSif1.vu32txMQPopLen++]);
            if (g_stcUart.stcSif1.vu32txMQPopLen >= USART_SIF1_MAX_TX_SIZE)
            {
                g_stcUart.stcSif1.vu32txMQPopLen = 0;
            }
        }
        else
        {
            g_stcUart.stcSif1.vu32txMQPopLen = 0;
            g_stcUart.stcSif1.vu32txMQPushLen = 0;			
            g_stcUart.stcSif1.btxEnable = false;
            USART_ConfigInt(USART3_CH, USART_INT_TXDE, DISABLE);
        }
    }	

}

/**
 * @brief  This function handles UART4 global interrupt request.
 * @param  none
 * @return none
 * @note   none
 */
void UART4_IRQHandler(void)
{
    if (USART_GetIntStatus(USART4_CH, USART_INT_RXDNE) != RESET)
    {
        /* Read one byte from the receive data register */
        g_stcUart.stcSif2.vu8rxData = USART_ReceiveData(USART4_CH);	
        g_stcUart.stcSif2.u8rxbuff[g_stcUart.stcSif2.vu32rxMQPush++] = g_stcUart.stcSif2.vu8rxData;
        if (g_stcUart.stcSif2.vu32rxMQPush >= USART_SIF2_MAX_RX_SIZE)
        {
            g_stcUart.stcSif2.vu32rxMQPush = 0;
        }
    }

    if (USART_GetIntStatus(USART4_CH, USART_INT_IDLEF) != RESET)
    {	
        USART_ReceiveData(USART4_CH);/*读取清标志，只做清楚标志用*/
        g_stcUart.stcSif2.brxCompleteFlg = true;
    }	
	
    if(USART_GetIntStatus(USART4_CH, USART_INT_TXDE) != RESET)
    {   
        /* Write one byte to the transmit data register */
        USART_ClrFlag( USART4_CH, USART_INT_TXDE );
		
        if (g_stcUart.stcSif2.vu32txMQPushLen > g_stcUart.stcSif2.vu32txMQPopLen)
        {
            USART_SendData(USART4_CH, g_stcUart.stcSif2.u8txbuff[g_stcUart.stcSif2.vu32txMQPopLen++]);
            if (g_stcUart.stcSif2.vu32txMQPopLen >= USART_SIF2_MAX_TX_SIZE)
            {
                g_stcUart.stcSif2.vu32txMQPopLen = 0;
            }
        }
        else
        {
            g_stcUart.stcSif2.vu32txMQPopLen = 0;
            g_stcUart.stcSif2.vu32txMQPushLen = 0;			
            g_stcUart.stcSif2.btxEnable = false;
            USART_ConfigInt(USART4_CH, USART_INT_TXDE, DISABLE);
        }
    }	

}

/**
 * @brief  This function handles UART5 global interrupt request.
 * @param  none
 * @return none
 * @note   none
 */
void UART5_IRQHandler(void)
{
    if (USART_GetIntStatus(USART5_CH, USART_INT_RXDNE) != RESET)
    {
        /* Read one byte from the receive data register */
        g_stcUart.stcLog.vu8rxData = USART_ReceiveData(USART5_CH);	
        g_stcUart.stcLog.u8rxbuff[g_stcUart.stcLog.vu32rxMQPush++] = g_stcUart.stcLog.vu8rxData;
        if (g_stcUart.stcLog.vu32rxMQPush >= USART_LOG_MAX_RX_SIZE)
        {
            g_stcUart.stcLog.vu32rxMQPush = 0;
        }
    }

    if (USART_GetIntStatus(USART5_CH, USART_INT_IDLEF) != RESET)
    {	
        USART_ReceiveData(USART5_CH);/*读取清标志，只做清楚标志用*/
        g_stcUart.stcLog.brxCompleteFlg = true;
    }	
	
    if(USART_GetIntStatus(USART5_CH, USART_INT_TXDE) != RESET)
    {   
        /* Write one byte to the transmit data register */
        USART_ClrFlag( USART5_CH, USART_INT_TXDE );
		
        if (g_stcUart.stcLog.vu32txMQPushLen > g_stcUart.stcLog.vu32txMQPopLen)
        {
            USART_SendData(USART5_CH, g_stcUart.stcLog.u8txbuff[g_stcUart.stcLog.vu32txMQPopLen++]);
            if (g_stcUart.stcLog.vu32txMQPopLen >= USART_LOG_MAX_TX_SIZE)
            {
                g_stcUart.stcLog.vu32txMQPopLen = 0;
            }
        }
        else
        {
            g_stcUart.stcLog.vu32txMQPopLen = 0;
            g_stcUart.stcLog.vu32txMQPushLen = 0;			
            g_stcUart.stcLog.btxEnable = false;
            USART_ConfigInt(USART5_CH, USART_INT_TXDE, DISABLE);
        }
    }	

}

/**
 * @brief  driver usart1 initialization
 * @param  baudrate : the commu baud rate
 * @return none
 * @note   none
 */
void drv_usart1_init(uint32_t baudrate)
{
    NVIC_InitType NVIC_InitStructure;
    GPIO_InitType GPIO_InitStructure;
    USART_InitType USART_InitStructure;	
	
    /* Enable GPIO clock */
    RCC_EnableAPB2PeriphClk(USART1_GPIO_CLK, ENABLE);
    /* Enable USARTy and USARTz Clock */
    USART1_APBxClkCmd(USART1_CLK, ENABLE);

    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel           = USART1_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd        = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Initialize GPIO_InitStructure */
    GPIO_InitStruct(&GPIO_InitStructure);
	
    /* Configure USARTy Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USART1_TX_PIN;
    GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Alternate = USART1_Tx_GPIO_AF;
    GPIO_InitPeripheral(USART1_TX_PORT, &GPIO_InitStructure);
	
    /* Configure USARTx Rx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USART1_RX_PIN;
    GPIO_InitStructure.GPIO_Alternate = USART1_Rx_GPIO_AF;	
    GPIO_InitPeripheral(USART1_RX_PORT, &GPIO_InitStructure);  

    /* USARTy and USARTz configuration -----------------------------------------------------*/
    USART_InitStructure.BaudRate            = baudrate;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USARTy and USARTz */
    USART_Init(USART1_CH, &USART_InitStructure);
    /* Enable USARTy Receive and Transmit interrupts */
    USART_ConfigInt(USART1_CH, USART_INT_RXDNE, ENABLE);
    USART_ConfigInt(USART1_CH, USART_INT_IDLEF, ENABLE);		
    //USART_ConfigInt(USART1_CH, USART_INT_TXDE, ENABLE);
    /* Enable the USARTy and USARTz */
    USART_Enable(USART1_CH, ENABLE);
}

/**
 * @brief  driver usart3 initialization
 * @param  baudrate : the commu baud rate
 * @return none
 * @note   none
 */
void drv_usart3_init(uint32_t baudrate)
{
    NVIC_InitType NVIC_InitStructure;
    GPIO_InitType GPIO_InitStructure;
    USART_InitType USART_InitStructure;	
	
    /* Enable GPIO clock */
    RCC_EnableAPB2PeriphClk(USART3_GPIO_CLK, ENABLE);
    /* Enable USARTy and USARTz Clock */
    USART3_APBxClkCmd(USART3_CLK, ENABLE);

    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel           = USART3_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd        = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Initialize GPIO_InitStructure */
    GPIO_InitStruct(&GPIO_InitStructure);
	
    /* Configure USARTy Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USART3_TX_PIN;
    GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Alternate = USART3_Tx_GPIO_AF;
    GPIO_InitPeripheral(USART3_TX_PORT, &GPIO_InitStructure);
	
    /* Configure USARTx Rx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USART3_RX_PIN;
    GPIO_InitStructure.GPIO_Alternate = USART3_Rx_GPIO_AF;	
    GPIO_InitPeripheral(USART3_RX_PORT, &GPIO_InitStructure);  

    /* USARTy and USARTz configuration -----------------------------------------------------*/
    USART_InitStructure.BaudRate            = baudrate;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USARTy and USARTz */
    USART_Init(USART3_CH, &USART_InitStructure);
    /* Enable USARTy Receive and Transmit interrupts */
    USART_ConfigInt(USART3_CH, USART_INT_RXDNE, ENABLE);
    USART_ConfigInt(USART3_CH, USART_INT_IDLEF, ENABLE);		
    //USART_ConfigInt(USART3_CH, USART_INT_TXDE, ENABLE);
    /* Enable the USARTy and USARTz */
    USART_Enable(USART3_CH, ENABLE);

}

/**
 * @brief  driver usart4 initialization
 * @param  baudrate : the commu baud rate
 * @return none
 * @note   none
 */
void drv_usart4_init(uint32_t baudrate)
{
    NVIC_InitType NVIC_InitStructure;
    GPIO_InitType GPIO_InitStructure;
    USART_InitType USART_InitStructure;	
	
    /* Enable GPIO clock */
    RCC_EnableAPB2PeriphClk(USART4_GPIO_CLK, ENABLE);
    /* Enable USARTy and USARTz Clock */
    USART4_APBxClkCmd(USART4_CLK, ENABLE);

    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel           = UART4_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelCmd        = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Initialize GPIO_InitStructure */
    GPIO_InitStruct(&GPIO_InitStructure);
	
    /* Configure USARTy Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USART4_TX_PIN;
    GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Alternate = USART4_Tx_GPIO_AF;
    GPIO_InitPeripheral(USART4_TX_PORT, &GPIO_InitStructure);
	
    /* Configure USARTx Rx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USART4_RX_PIN;
    GPIO_InitStructure.GPIO_Alternate = USART4_Rx_GPIO_AF;	
    GPIO_InitPeripheral(USART4_RX_PORT, &GPIO_InitStructure);  

    /* USARTy and USARTz configuration -----------------------------------------------------*/
    USART_InitStructure.BaudRate            = baudrate;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USARTy and USARTz */
    USART_Init(USART4_CH, &USART_InitStructure);
    /* Enable USARTy Receive and Transmit interrupts */
    USART_ConfigInt(USART4_CH, USART_INT_RXDNE, ENABLE);
    USART_ConfigInt(USART4_CH, USART_INT_IDLEF, ENABLE);		
    //USART_ConfigInt(USART4_CH, USART_INT_TXDE, ENABLE);
    /* Enable the USARTy and USARTz */
    USART_Enable(USART4_CH, ENABLE);

}

/**
 * @brief  driver usart5 initialization
 * @param  baudrate : the commu baud rate
 * @return none
 * @note   none
 */
void drv_usart5_init(uint32_t baudrate)
{
    NVIC_InitType NVIC_InitStructure;
    GPIO_InitType GPIO_InitStructure;
    USART_InitType USART_InitStructure;	
	
    /* Enable GPIO clock */
    RCC_EnableAPB2PeriphClk(USART5_GPIO_CLK, ENABLE);
    /* Enable USARTy and USARTz Clock */
    USART5_APBxClkCmd(USART5_CLK, ENABLE);

    /* Enable the USARTy Interrupt */
    NVIC_InitStructure.NVIC_IRQChannel           = UART5_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 6;
    NVIC_InitStructure.NVIC_IRQChannelCmd        = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    /* Initialize GPIO_InitStructure */
    GPIO_InitStruct(&GPIO_InitStructure);
	
    /* Configure USARTy Tx as alternate function push-pull */
    GPIO_InitStructure.Pin            = USART5_TX_PIN;
    GPIO_InitStructure.GPIO_Mode      = GPIO_Mode_AF_PP;
		GPIO_InitStructure.GPIO_Alternate = USART5_Tx_GPIO_AF;
    GPIO_InitPeripheral(USART5_TX_PORT, &GPIO_InitStructure);
	
//    /* Configure USARTx Rx as alternate function push-pull */
//    GPIO_InitStructure.Pin            = USART5_RX_PIN;
//    GPIO_InitStructure.GPIO_Alternate = USART5_Rx_GPIO_AF;	
//    GPIO_InitPeripheral(USART5_RX_PORT, &GPIO_InitStructure);  

    /* USARTy and USARTz configuration -----------------------------------------------------*/
    USART_InitStructure.BaudRate            = baudrate;
    USART_InitStructure.WordLength          = USART_WL_8B;
    USART_InitStructure.StopBits            = USART_STPB_1;
    USART_InitStructure.Parity              = USART_PE_NO;
    USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;
    USART_InitStructure.Mode                = USART_MODE_RX | USART_MODE_TX;

    /* Configure USARTy and USARTz */
    USART_Init(USART5_CH, &USART_InitStructure);
    /* Enable USARTy Receive and Transmit interrupts */
    //USART_ConfigInt(USART5_CH, USART_INT_RXDNE, ENABLE);
    //USART_ConfigInt(USART5_CH, USART_INT_IDLEF, ENABLE);		
    //USART_ConfigInt(USART5_CH, USART_INT_TXDE, ENABLE);
    /* Enable the USARTy and USARTz */
    USART_Enable(USART5_CH, ENABLE);

}


/**
 * @brief  usart send data user int
 * @param  UartIndex:  the usart channel
 *         p:the data head 
 *         len:the data send len 
 * @return none
 * @note   none
 */
void drv_usart_sendData(uint8_t UartIndex, uint8_t *p, uint32_t len)
{
    if (UartIndex == USART_LOG_INDEX)
    {
        if ((g_stcUart.stcLog.btxEnable)&&(g_stcUart.stcLog.vu32txMQPushLen + len >= USART_LOG_MAX_TX_SIZE))
        {
            return ;
        }
		
		    if (g_stcUart.stcLog.vu32txMQPushLen + len >= USART_LOG_MAX_TX_SIZE)/*保护*/
		    {
			      g_stcUart.stcLog.vu32txMQPushLen = 0;
		    }

		    memcpy(&g_stcUart.stcLog.u8txbuff[g_stcUart.stcLog.vu32txMQPushLen],p,len);
		    g_stcUart.stcLog.vu32txMQPushLen += len;
		
        if( g_stcUart.stcLog.btxEnable != true)
        {
            g_stcUart.stcLog.btxEnable = true;        
            g_stcUart.stcLog.vu32txMQPopLen = 0;
            g_stcUart.stcLog.vu32txMQPushLen = len;				
            USART_ConfigInt(USART5_CH, USART_INT_TXDE, ENABLE);
        }	
    }	
    else if (UartIndex == USART_RS485_INDEX)
    {
        if ((g_stcUart.stcRs485.btxEnable)&&(g_stcUart.stcRs485.vu32txMQPushLen + len >= USART_RS485_MAX_TX_SIZE))
        {
            return ;
        }
		
		    if (g_stcUart.stcRs485.vu32txMQPushLen + len >= USART_RS485_MAX_TX_SIZE)/*保护*/
		    {
			      g_stcUart.stcRs485.vu32txMQPushLen = 0;
		    }

		    memcpy(&g_stcUart.stcRs485.u8txbuff[g_stcUart.stcRs485.vu32txMQPushLen],p,len);
		    g_stcUart.stcRs485.vu32txMQPushLen += len;
		
        if( g_stcUart.stcRs485.btxEnable != true)
        {
            g_stcUart.stcRs485.btxEnable = true;        
            g_stcUart.stcRs485.vu32txMQPopLen = 0;
            g_stcUart.stcRs485.vu32txMQPushLen = len;				
            USART_ConfigInt(USART1_CH, USART_INT_TXDE, ENABLE);
        }	
    }
    else if (UartIndex == USART_SIF1_INDEX)
    {
        if ((g_stcUart.stcSif1.btxEnable)&&(g_stcUart.stcSif1.vu32txMQPushLen + len >= USART_SIF1_MAX_TX_SIZE))
        {
            return ;
        }
		
		    if (g_stcUart.stcSif1.vu32txMQPushLen + len >= USART_SIF1_MAX_TX_SIZE)/*保护*/
		    {
			      g_stcUart.stcSif1.vu32txMQPushLen = 0;
		    }

		    memcpy(&g_stcUart.stcSif1.u8txbuff[g_stcUart.stcSif1.vu32txMQPushLen],p,len);
		    g_stcUart.stcSif1.vu32txMQPushLen += len;
		
        if( g_stcUart.stcSif1.btxEnable != true)
        {
            g_stcUart.stcSif1.btxEnable = true;        
            g_stcUart.stcSif1.vu32txMQPopLen = 0;
            g_stcUart.stcSif1.vu32txMQPushLen = len;				
            USART_ConfigInt(USART3_CH, USART_INT_TXDE, ENABLE);
        }	
    }	
    else if (UartIndex == USART_SIF2_INDEX)
    {
        if ((g_stcUart.stcSif2.btxEnable)&&(g_stcUart.stcSif2.vu32txMQPushLen + len >= USART_SIF2_MAX_TX_SIZE))
        {
            return ;
        }
		
		    if (g_stcUart.stcSif2.vu32txMQPushLen + len >= USART_SIF2_MAX_TX_SIZE)/*保护*/
		    {
			      g_stcUart.stcSif2.vu32txMQPushLen = 0;
		    }

		    memcpy(&g_stcUart.stcSif2.u8txbuff[g_stcUart.stcSif2.vu32txMQPushLen],p,len);
		    g_stcUart.stcSif2.vu32txMQPushLen += len;
		
        if( g_stcUart.stcSif2.btxEnable != true)
        {
            g_stcUart.stcSif2.btxEnable = true;        
            g_stcUart.stcSif2.vu32txMQPopLen = 0;
            g_stcUart.stcSif2.vu32txMQPushLen = len;				
            USART_ConfigInt(USART4_CH, USART_INT_TXDE, ENABLE);
        }	
    }		
    else
    {

    }

}

/**
 * @brief  usart sleep
 * @param  none
 * @return none
 * @note   none
 */
void drv_usart_sleep(void)
{
	  drv_gpio_outputInit(USART1_TX_PORT, USART1_TX_PIN,GPIO_Mode_Out_PP); 
	  drv_gpio_outputInit(USART1_RX_PORT, USART1_RX_PIN,GPIO_Mode_Out_PP); 
	  drv_gpio_outputInit(USART5_TX_PORT, USART5_TX_PIN,GPIO_Mode_Out_PP); 
	  //drv_gpio_outputInit(USART5_RX_PORT, USART5_RX_PIN,GPIO_Mode_Out_PP); 
	  drv_gpio_outputInit(USART3_TX_PORT, USART3_TX_PIN,GPIO_Mode_Out_PP); 
	  drv_gpio_outputInit(USART3_RX_PORT, USART3_RX_PIN,GPIO_Mode_Out_PP); 	
	  drv_gpio_outputInit(USART4_TX_PORT, USART4_TX_PIN,GPIO_Mode_Out_PP); 
	  drv_gpio_outputInit(USART4_RX_PORT, USART4_RX_PIN,GPIO_Mode_Out_PP); 	
	
		GPIO_SetBits(USART1_TX_PORT,USART1_TX_PIN);
		GPIO_SetBits(USART1_RX_PORT,USART1_RX_PIN);
	
		GPIO_SetBits(USART5_TX_PORT,USART5_TX_PIN);
		//GPIO_SetBits(USART5_RX_PORT,USART5_RX_PIN);
		GPIO_ResetBits(USART3_TX_PORT,USART3_TX_PIN);
		GPIO_ResetBits(USART3_RX_PORT,USART3_RX_PIN);	
	
		GPIO_ResetBits(USART4_TX_PORT,USART4_TX_PIN);
		GPIO_ResetBits(USART4_RX_PORT,USART4_RX_PIN);	
	
}

/**
 * @brief  usart wakeup
 * @param  none
 * @return none
 * @note   none
 */
void drv_usart_wakeup(void)
{
    drv_usart_init();
}

/**
 * @brief  driver usart timeout dealwith
 * @param  none
 * @return none
 * @note   none
 */
void drv_usart_timeout(void)
{
    if (g_stcUart.stcRs485.u8txCompleteTimeout)
	  {
        g_stcUart.stcRs485.u8txCompleteTimeout--;
		    if (g_stcUart.stcRs485.u8txCompleteTimeout == 0)
		    {
            /* Write one byte to the transmit data register */
            USART_ClrFlag( USART1_CH, USART_INT_TXC );
		        USART_ConfigInt(USART1_CH, USART_INT_TXC, DISABLE);/*关闭发送完成中断检测*/
//		        RS485_RECEIVE();		
		    }
	  }
}

/**
 * @brief  driver usart initialization
 * @param  none
 * @return none
 * @note   none
 */
void drv_usart_init(void)
{
	  /*stcLog*/    
    drv_usart5_init(USART5_BAUDRATE);
    /*stcRs485*/    
    drv_usart1_init(USART1_BAUDRATE);	
    /*stcSif1*/    
    drv_usart3_init(USART3_BAUDRATE);	
    /*stcSif2*/    
    drv_usart4_init(USART4_BAUDRATE);	
	
	  memset(&g_stcUart,0x00,sizeof(stc_uart_t));
	
}

// =======================================================================================
// End of file.
// =======================================================================================
